<?php

class store extends cms_controller {

    var $stepsname = array(
        1 => 'accept',
        2 => 'select',
        3 => 'payment'
    );
    var $first = true;

    function __construct() {
        parent::__construct();
        $this->load->model('store_model', 'stores');
        $this->load->model('store_details_model', 'stdetails');
        $this->load->model('products_model', 'products');
    }

    function index($id = false) {
        if ($id) {
            $store = $this->stores->is_found(array('id' => $id), array(), 'row', $this->agent->referrer());
            $this->stores->incrment(array('id' => $id));
        } else {
            $store = $this->stores->is_found(array('owner_id' => $this->user_id), array(), 'row', __CLASS__ . '/create/1', 'message_create_yourstore');
            $id = $store->id;
        }
        if ($store->active == 'NO') {
            if ($store->owner_id == $this->user_id) {
                $this->pass['notactive_owner'] = 'ss';
            } else {
                $this->pass['notactive'] = 'ss';
            }
        } else {
            $this->pass['storedetails'] = $this->stdetails->get(array('store_id' => $id));
        }

        if ($store->owner_id == $this->user_id) {
            $this->pass['owner'] = true;
        }
        $this->pass['store'] = $store;
        $this->pass['products'] = $this->products->get(array('store_id' => $id), false, 'result', 8);
        $this->pass['specialproducts'] = $this->products->get(array('store_id' => $id, 'special_product' => 'YES'));

        $this->themes->render(__CLASS__ . '/index', $this->pass, false, 'wide.php');
    }

    function create($step = false) {


        if (in_array($step, array(1, 2, 3, 'finish'))) {
            if ($step != 1) {
                $this->checksteps($step);
            }
            switch ($step) {
                case 1:
                    $this->stepone();
                    break;
                case 2:
                    $this->steptwo();
                    break;
                case 3:
                    $this->stepthree();
                    break;
                case 'finish':
                    $this->finish();
                    break;
                default :
                    show_404();
                    break;
            }
        } else {
            show_404();
        }
    }

    private function checksteps($current) {
        $session = $this->stepsname;
        if (!$this->session->userdata('steps')) {
            redirect(__CLASS__ . '/create/1');
            return;
        } else {
            $steps = $this->session->userdata('steps');

            switch ($current) {
                case 1:
                    return TRUE;
                    break;
                case 2:
                    if (!in_array($session[1], $steps))
                        redirect(__CLASS__ . '/create/1');
                    break;
                case 3:
                    if (!in_array($session[1], $steps) && !in_array($session[2], $steps))
                        redirect(__CLASS__ . '/create/2');
                    break;
                case 'finish':
                    if (!in_array($session[1], $steps) && !in_array($session[2], $steps) && !in_array($session[3], $steps))
                        redirect(__CLASS__ . '/create/3');
                    break;

                default :
                    show_404();
                    break;
            }
        }
    }

    private function setstep($step) {
        $session = $this->stepsname;
        $steps = array();
        if (!$this->session->userdata('steps')) {
            if ($step == 1) {
                if (!in_array($session[$step], $steps)) {
                    $steps[] = $session[$step];
                    $this->session->set_userdata('steps', $steps);
                }
            } else {
                redirect(__CLASS__ . '/create/1');
            }
        } else {
            if ($step != 'finish') {
                $steps = $this->session->userdata('steps');
                if (!in_array($session[$step], $steps)) {
                    $steps[] = $session[$step];
                    $this->session->set_userdata('steps', $steps);
                }
            }
        }
    }

    private function stepone() {
        if ($this->input->post()) {
            if ($this->input->post('terms') == 'accept') {
                $this->setstep('1');
                $post = array('terms' => 'accept');
                $this->session->set_userdata('post', $post);
                redirect(__CLASS__ . '/create/2');
            } elseif ($this->input->post('terms') == 'refuse') {
                $this->themes->set_message(array('url' => '', 'type' => 'error', 'message' => 'message_refuse_term'));
            } else {
                $this->themes->set_message(array('url' => current_url(), 'type' => 'error', 'message' => 'message_read_term'));
            }
        } else {
            $st = $this->stores->get(array('owner_id' => $this->user_id));
            if ($st) {
                redirect(__CLASS__);
                return;
            }
            $this->load->model('pages_model', 'page');
            $this->pass['data'] = $this->page->get(array('slug' => 'terms'), false, 'row');
            $this->themes->render(__CLASS__ . '/stepone', $this->pass);
        }
    }

    private function steptwo() {
        $this->load->model('packages_details_model', 'pdetails');
        $this->load->model('packages_model', 'packages');
        $this->pass['packages'] = $this->packages->get();
        $this->pass['pdetails'] = $this->pdetails->get();

        if ($this->input->post()) {
            if ($this->input->post('package')) {
                $data = $this->packages->is_found(array('id' => $this->input->post('package')), array(), 'row', current_url());
                $store['owner_id'] = $this->user_id;
                $store['package_id'] = $data->id;
                $store['package_values'] = $data->values;
                $store['package_price'] = $data->price;
                $store['package_title'] = $data->title;
                if ($data->price == 0) {
                    $store['active'] = 'YES';
                    $store['start_date'] = date("Y-m-d H:i:s");
                }
                $this->session->set_userdata('store', $store);
                $this->setstep('2');
                $post = $this->session->userdata('post');
                $post = array('package' => $this->input->post('package'));
                $this->session->set_userdata('post', $post);
                redirect(__CLASS__ . '/create/3');
            } else {
                $this->themes->set_message(array('url' => current_url(), 'type' => 'error', 'message' => 'message_no_package_selected'));
            }
        }

        $this->themes->render(__CLASS__ . '/steptwo', $this->pass);
    }

    private function stepthree() {
        $store = $this->session->userdata('store');
        if ($store['package_price'] == 0) {
            if ($this->stores->save($store)) {
                $this->setstep('3');
                redirect(__CLASS__ . '/create/finish');
            }
            return;
        }
        if ($this->input->post()) {
            $this->load->model('invoicies_model', 'invoice');
            $string = md5($store['owner_id'] + $store['package_id'] + $store['package_price'] + $store['package_title'] + time());
            $invoice['inv_num'] = substr($string, 0, 9);
            $invoice['user_id'] = $store['owner_id'];
            $invoice['total'] = $store['package_price'];
            $invoice['must_pay'] = $store['package_price'];
            $invoice['title'] = 'message_store_invoice';
            $invoice['for'] = 'create_store';
            if ($this->invoice->save($invoice)) {
                $store['invoice_id'] = $invoice['inv_num'];
                if ($this->stores->save($store)) {
                    $this->setstep('3');
                    redirect(__CLASS__ . '/create/finish');
                }
            }
        }
        $this->load->model('banks_model', 'banks');
        $this->pass['banks'] = $this->banks->get();
        $this->pass['store'] = $store;
        $this->themes->render(__CLASS__ . '/stepthree', $this->pass);
    }

    private function finish() {
        if ($this->session->userdata('store') && $this->session->userdata('post')) {
            $data = $this->session->userdata('store');
            $this->session->unset_userdata('store');
            $this->session->unset_userdata('post');
            if ($data['package_price'] == 0) {
                $this->pass['free'] = 1;
            }
            $this->themes->render(__CLASS__ . '/finish', $this->pass);
        } else {
            redirect(__CLASS__);
        }
    }

    private function upload_file($input) {

        $config['upload_path'] = STORE_PATH . '/temp/';
        $config['file_name'] = md5(time() . $this->session->userdata('user_NAME') . $input);
        $config['allowed_types'] = 'gif|jpg|png|jpeg';
        $config['max_size'] = '1024';
        $config['max_width'] = '1024';
        $config['max_height'] = '768';
        if ($this->first) {
            $this->load->library('upload', $config);
            $this->first = false;
        } else {
            $this->upload->initialize($config);
        }

        if (!$this->upload->do_upload($input)) {
            return $this->upload->display_errors();
        } else {
            $filedata = $this->upload->data();
            $this->session->set_userdata($input, $filedata['file_name']);
            return true;
        }
    }

    function deleteimage($name = false) {
        if ($name) {
            $store = $this->stores->get(array('owner_id' => $this->user_id), false, 'row');
            if ($store) {
                if (in_array($name, array('storebanner_1', 'storebanner_2', 'storebanner_3', 'storead_1', 'storead_2'))) {
                    $details = $this->stdetails->get(array('name' => $name, 'store_id' => $store->id), false, 'row');
                    if ($this->stdetails->delete(array('name' => $name, 'store_id' => $store->id), FALSE)) {
                        @unlink(STORE_PATH . '/' . $details->value);
                        die(lang('message_delete_success'));
                    }
                }
            }
        }
        die('no');
    }

    public function edit() {
        if ($id = $this->session->userdata('user_ID')) {
            $this->pass['store'] = $store = $this->stores->is_found(array('owner_id' => $id), array(), 'row', __CLASS__);
            $this->pass['storedetails'] = $details = get_store_details($this->stdetails->get(array('store_id' => $store->id)));
            $this->load->model('packages_details_model');
            $this->pass['pdetails'] = $pdetails = $this->packages_details_model->get(false, FALSE, 'result', FALSE, FALSE, false, array('pd_id' => 'asc'));
            if ($this->input->post()) {
                $this->session->set_userdata('post', $this->input->post());
                $this->load->library('form_validation');
                $package = unserialize($store->package_values);
                foreach ($pdetails as $pd) {
                    if ($pd->type != 'no' && $package[$pd->name] != 'false') {
                        if ($pd->name == 'storename')
                            if (!isset($details['storename']) || $details['storename'] != $this->input->post('storename')) {
                                $this->form_validation->set_rules($pd->name, $pd->title, 'required|alpha_dash|is_unique[store_details.value]');
                            } else {
                                $this->form_validation->set_rules($pd->name, $pd->title, 'required|alpha_dash');
                            }
                    }
                }


                if ($this->form_validation->run()) {
                    $upload = $this->upload_file('logo');
                    if ($upload !== true && !isset($details['logo'])) {
                        $this->themes->set_message(array('url' => current_url(), 'type' => 'error', 'message' => $upload));
                    }
                    $upload = null;
                    $storebanner = 0;
                    if ($package['storebanner'] == 'banner')
                        $storebanner = 1;
                    elseif ($package['storebanner'] == 'slider')
                        $storebanner = 3;
                    for ($i = 1; $i <= $storebanner; $i++) {
                        $upload = NULL;
                        $upload = $this->upload_file("storebanner_$i");
                    }

                    $upload = NULL;
                    $storead = 0;
                    if ($package['storead'] == 'true_owner')
                        $storead = 2;
                    for ($i = 1; $i <= $storead; $i++) {
                        $upload = NULL;
                        $upload = $this->upload_file("storead_$i");
                    }
                    $save['store_id'] = $store->id;
                    foreach ($package as $key => $value) {

                        if ($key == 'logo') {
                            if ($this->session->userdata('logo')) {
                                if (@copy(STORE_PATH . "/temp/" . $this->session->userdata('logo'), STORE_PATH . "/" . $this->session->userdata('logo'))) {
                                    @unlink(STORE_PATH . "/temp/" . $this->session->userdata('logo'));
                                    $this->stdetails->delete(array('name' => 'logo', 'store_id' => $store->id), FALSE);
                                    $save['name'] = $key;
                                    $save['value'] = $this->session->userdata('logo');
                                    $this->stdetails->save($save);
                                    $this->session->unset_userdata('logo');
                                }
                            }
                        } elseif ($key == 'storebanner') {
                            for ($i = 1; $i <= $storebanner; $i++) {
                                if (@copy(STORE_PATH . "/temp/" . $this->session->userdata("storebanner_$i"), STORE_PATH . "/" . $this->session->userdata("storebanner_$i"))) {
                                    @unlink(STORE_PATH . "/temp/" . $this->session->userdata("storebanner_$i"));
                                    $this->stdetails->delete(array('name' => "storebanner_$i", 'store_id' => $store->id), FALSE);
                                    $save['name'] = "storebanner_$i";
                                    $save['value'] = $this->session->userdata("storebanner_$i");
                                    $this->stdetails->save($save);
                                    $this->session->unset_userdata("storebanner_$i");
                                }
                            }
                        } elseif ($key == 'storead') {
                            for ($i = 1; $i <= $storead; $i++) {
                                if (@copy(STORE_PATH . "/temp/" . $this->session->userdata("storead_$i"), STORE_PATH . "/" . $this->session->userdata("storead_$i"))) {
                                    @unlink(STORE_PATH . "/temp/" . $this->session->userdata("storead_$i"));
                                    $this->stdetails->delete(array('name' => "storead_$i", 'store_id' => $store->id), FALSE);
                                    $save['name'] = "storead_$i";
                                    $save['value'] = $this->session->userdata("storead_$i");
                                    $this->stdetails->save($save);
                                    $this->session->unset_userdata("storead_$i");
                                }
                            }
                        } elseif ($key == 'staticpages') {
                            $title = $this->input->post('staticpages_title');
                            $content = $this->input->post('staticpages_content');
                            $value = explode('+', $value);
                            $input = 1;
                            if (count($value) > 0) {
                                foreach ($value as $p => $v) {
                                    $v = trim($v);
                                    if (is_numeric($v)) {
                                        for ($i = $input; $i <= $v + $input; $i++) {
                                            if (isset($title[$i]) && isset($content[$i]) && $title[$i] && $content[$i]) {
                                                $this->stdetails->delete(array('name' => "staticpages_$i", 'store_id' => $store->id), FALSE);
                                                $save['name'] = "staticpages_$i";
                                                $page['title'] = $title[$i];
                                                $page['content'] = $content[$i];
                                                $save['value'] = serialize($page);
                                                $this->stdetails->save($save);
                                            } else {
                                                $this->stdetails->delete(array('name' => "staticpages_$i", 'store_id' => $store->id), FALSE);
                                            }
                                        }
                                        $input += $i;
                                    } else {
                                        if ($title[$v] && $content[$v]) {
                                            $this->stdetails->delete(array('name' => "staticpages_$v", 'store_id' => $store->id), FALSE);
                                            $save['name'] = "staticpages_$v";
                                            $page['title'] = $title[$v];
                                            $page['content'] = $content[$v];
                                            $save['value'] = serialize($page);
                                            $this->stdetails->save($save);
                                        } else {
                                            $this->stdetails->delete(array('name' => "staticpages_$v", 'store_id' => $store->id), FALSE);
                                        }
                                        $input++;
                                    }
                                }
                            }
                        } elseif ($key == 'storelink') {
                            $this->stdetails->delete(array('name' => "$key", 'store_id' => $store->id), FALSE);
                            $save['name'] = "$key";
                            $save['value'] = $this->input->post("storename");
                            $this->stdetails->save($save);
                        } else {
                            if ($this->input->post("$key")) {
                                $this->stdetails->delete(array('name' => "$key", 'store_id' => $store->id), FALSE);
                                $save['name'] = "$key";
                                $save['value'] = $this->input->post("$key");
                                $this->stdetails->save($save);
                            } else {
                                $this->stdetails->delete(array('name' => "$key", 'store_id' => $store->id), FALSE);
                            }
                        }
                    }
                    $this->session->unset_userdata('post');
                    $this->themes->set_message(array('type' => 'success', 'url' => __CLASS__, 'message' => 'message_save_success'));
                } else {
                    get_validation_errors();
                }
            }

            $this->themes->render('store/edit', $this->pass);
        }
        else
            show_404();
    }

    function subscribe($id = false) {
        if ($id) {
            if (!user_subcribe_store($id)) {
                $store = $this->stores->is_found(array('id' => $id), array(), 'row', $this->agent->referrer());
                if ($this->user_id) {
                    if ($store->owner_id == $this->user_id) {
                        $this->themes->set_message(array('type' => 'error', 'url' => __CLASS__, 'message' => 'message_you_are_owner'));
                    } else {
                        if (subscribe_user_store($this->user_id, $id))
                            $this->themes->set_message(array('type' => 'success', 'url' => $this->agent->referrer(), 'message' => 'message_save_success'));
                        else
                            $this->themes->set_message(array('type' => 'error', 'url' => $this->agent->referrer(), 'message' => 'message_save_error'));
                    }
                }
            }else {
                $this->themes->set_message(array('type' => 'success', 'url' => $this->agent->referrer(), 'message' => 'message_save_success'));
            }
        } else {
            show_404();
        }
    }

    function unsubscribe($id = false, $user = false) {
        if ($id) {
            if (user_subcribe_store($id)) {
                if (is_numeric($user)) {
                    if (unsubscribe_user_store($user, $id))
                        $this->themes->set_message(array('type' => 'success', 'url' => $this->agent->referrer(), 'message' => 'message_save_success'));
                    else
                        $this->themes->set_message(array('type' => 'error', 'url' => $this->agent->referrer(), 'message' => 'message_save_error'));
                }elseif ($this->user_id) {
                    if (unsubscribe_user_store($this->user_id, $id))
                        $this->themes->set_message(array('type' => 'success', 'url' => $this->agent->referrer(), 'message' => 'message_save_success'));
                    else
                        $this->themes->set_message(array('type' => 'error', 'url' => $this->agent->referrer(), 'message' => 'message_save_error'));
                }
            }else {
                $this->themes->set_message(array('type' => 'success', 'url' => $this->agent->referrer(), 'message' => 'message_save_success'));
            }
        } else {
            show_404();
        }
    }

    function like($id = false) {
        if ($id) {
            if (!user_like_dislike_store($id)) {
                $store = $this->stores->is_found(array('id' => $id), array(), 'row', $this->agent->referrer());
                if ($this->user_id) {
                    if ($store->owner_id == $this->user_id) {
                        $this->themes->set_message(array('type' => 'error', 'url' => __CLASS__, 'message' => 'message_you_are_owner'));
                    } else {
                        if (like_user_store($this->user_id, $id))
                            $this->themes->set_message(array('type' => 'success', 'url' => $this->agent->referrer(), 'message' => 'message_save_success'));
                        else
                            $this->themes->set_message(array('type' => 'error', 'url' => $this->agent->referrer(), 'message' => 'message_save_error'));
                    }
                }
            }else {
                $this->themes->set_message(array('type' => 'success', 'url' => $this->agent->referrer(), 'message' => 'message_save_success'));
            }
        } else {
            show_404();
        }
    }

    function dislike($id = false) {
        if ($id) {
            if (!user_like_dislike_store($id)) {
                $store = $this->stores->is_found(array('id' => $id), array(), 'row', $this->agent->referrer());
                if ($this->user_id) {
                    if ($store->owner_id == $this->user_id) {
                        $this->themes->set_message(array('type' => 'error', 'url' => __CLASS__, 'message' => 'message_you_are_owner'));
                    } else {
                        if (dislike_user_store($this->user_id, $id))
                            $this->themes->set_message(array('type' => 'success', 'url' => $this->agent->referrer(), 'message' => 'message_save_success'));
                        else
                            $this->themes->set_message(array('type' => 'error', 'url' => $this->agent->referrer(), 'message' => 'message_save_error'));
                    }
                }
            }else {
                $this->themes->set_message(array('type' => 'success', 'url' => $this->agent->referrer(), 'message' => 'message_save_success'));
            }
        } else {
            show_404();
        }
    }

    function categories($storeid = false, $catid = false) {
        if (is_numeric($storeid) && is_numeric($catid)) {
            $this->pass['products'] = $this->products->get(array('store_id' => $storeid, 'subcat_id' => $catid));
            $this->themes->render('store/products', $this->pass);
        } else {
            show_404();
        }
    }

    function pages($storeid = false, $pagename = false) {
        if (is_numeric($storeid) && $pagename) {
            $this->pass['store'] = $store = $this->stores->is_found(array('id' => $storeid), array(), 'row', $this->agent->referrer());
            $this->pass['storename'] = $this->stdetails->get(array('store_id' => $storeid, 'name' => 'storename'), false, 'row');
            $this->pass['page'] = $page = $this->stdetails->get(array('store_id' => $storeid, 'name' => 'staticpages_' . $pagename), false, 'row');
            $this->themes->render('store/pages', $this->pass);
        } else {
            show_404();
        }
    }

    function search($storeid = false) {
        if ($storeid) {
            if ($this->input->post()) {
                $this->load->library('form_validation');
                $this->form_validation->set_rules('keyword', lang('label_search'), 'trim|required');
                if ($this->form_validation->run()) {
                    $this->pass['store'] = $store = $this->stores->is_found(array('id' => $storeid), array(), 'row', $this->agent->referrer());
                    $this->db->or_like('products.name', $this->input->post('keyword'));
                    $this->db->or_like('descrebtion', $this->input->post('keyword'));
                    $this->db->or_like('details', $this->input->post('keyword'));
                    $this->db->group_by('products.id');
                    $this->pass['products'] = $this->products->get(array('store_id' => $storeid));
                    $this->themes->render('store/search', $this->pass);
                } else {
                    $message['url'] = $this->agent->referrer();
                    $message['type'] = 'validation_error';
                    $message['message'] = validation_errors();
                    $this->themes->set_message($message);
                }
            } else {
                redirect($this->agent->referrer());
            }
        } else {
            show_404();
        }
    }

    function subscribers() {
        if ($this->user_id) {
            $this->pass['store'] = $store = $this->stores->is_found(array('owner_id' => $this->user_id), array(), 'row', $this->agent->referrer());
            $this->pass['subscribers'] = user_subcribe_store($store->id, TRUE);
            $this->themes->render('store/subscriber', $this->pass);
        } else {
            show_404();
        }
    }

    function deals() {
        if ($this->user_id) {
            $this->pass['store'] = $store = $this->stores->is_found(array('owner_id' => $this->user_id), array(), 'row', $this->agent->referrer());
            $this->load->model('seller_items_model', 'dealsmo');
            $this->pass['deals'] = $this->dealsmo->get(array('seller_id' => $store->id), FALSE, 'result', FALSE, FALSE, false, array('seen' => 'desc'));
            $this->themes->render('store/deals', $this->pass);
        } else {
            show_404();
        }
    }

    function showdeal($id = false) {
        if (is_numeric($id)) {
            $this->pass['store'] = $store = $this->stores->is_found(array('owner_id' => $this->user_id), array(), 'row', $this->agent->referrer());
            $this->load->model('invoicie_items_model');
            $this->pass['details'] = $details = $this->invoicie_items_model->get(array('id' => $id), FALSE, 'row');
            $this->load->model('seller_items_model', 'dealsmo');
            $this->pass['data'] = $data = $this->dealsmo->get(array('item_id' => $id), FALSE, 'row');
            if (count($details) <= 0) {
                redirect($this->agent->referrer());
            }
            if ($this->input->post('submit') == lang('accept_deal')) {

                if ($details->discount != $this->input->post('discount')) {
                    $this->load->library('form_validation');
                    $this->form_validation->set_rules('discount', lang('label_discount_total'), 'trim|numeric');
                    if ($this->form_validation->run()) {
                        $save['discount'] = $this->input->post('discount');
                        $save['total_price'] = $details->total_price - $save['discount'];
                        $this->invoicie_items_model->save($save, array('id' => $details->id), TRUE);
                        $this->load->model('invoicies_model', 'invoicies');
                        $invoice = $this->invoicies->get(array('inv_num' => $details->inv_num), FALSE, 'row');
                        $save = null;
                        $save['discount'] = $invoice->discount + $this->input->post('discount');
                        $save['must_pay'] = $invoice->must_pay - $this->input->post('discount');
                        $this->invoicies->save($save, array('id' => $invoice->id), TRUE);
                    } else {
                        get_validation_errors();
                    }
                }
                $save = NULL;
                $save['accept'] = 'YES';
                $save['canceled'] = 'NO';
                if ($this->dealsmo->save($save, array('inv_num' => $details->inv_num, 'item_id' => $details->id), TRUE)) {
                    $this->themes->set_message(array('type' => 'success', 'url' => current_url(), 'message' => 'message_save_success'));
                } else {
                    $this->themes->set_message(array('type' => 'error', 'url' => current_url(), 'message' => 'message_save_error'));
                }
            } elseif ($this->input->post('submit') == lang('cancel_deal')) {
                $this->load->model('invoicies_model', 'invoicies');
                $invoice = $this->invoicies->get(array('inv_num' => $details->inv_num), FALSE, 'row');
                $save['total'] = $invoice->total - ($details->price * $details->amount);
                $save['shiping'] = $invoice->shiping - $details->shiping;
                $save['discount'] = $invoice->discount - $details->discount;
                $save['must_pay'] = $invoice->must_pay - $details->total_price;
                $this->invoicies->save($save, array('id' => $invoice->id), TRUE);
                $save = NULL;
                $save['accept'] = 'NO';
                $save['canceled'] = 'YES';
                if ($this->dealsmo->save($save, array('inv_num' => $details->inv_num, 'item_id' => $details->id), TRUE)) {
                    $this->themes->set_message(array('type' => 'success', 'url' => current_url(), 'message' => 'message_save_success'));
                } else {
                    $this->themes->set_message(array('type' => 'error', 'url' => current_url(), 'message' => 'message_save_error'));
                }
            }
            if ($data->seen == 'NO') {
                $save = NULL;
                $save['seen'] = 'YES';
                $this->dealsmo->save($save, array('item_id' => $id), TRUE);
            }
            $this->themes->render('store/showdeal', $this->pass);
        } else {
            redirect($this->agent->referrer());
        }
    }

    function shipingdetails($id = FALSE) {
        if (is_numeric($id)) {
            $this->pass['store'] = $store = $this->stores->is_found(array('owner_id' => $this->user_id), array(), 'row', 'store');
            $this->load->model('seller_items_model', 'dealsmo');
            $data = $this->dealsmo->is_found(array('id' => $id), array(), 'row', 'store');
            if ($store->id != $data->seller_id) {
                $this->themes->set_message(array('type' => 'error', 'url' => 'store', 'message' => 'message_not_owner'));
            }
            $this->load->model('items_sent_model', 'itemsent');
            $shipingdetails = $this->itemsent->get(array('seller_items_id' => $id), false, 'row');
            $this->load->model('invoicie_items_model');
            $this->pass['details'] = $this->invoicie_items_model->get(array('id' => $data->item_id), FALSE, 'row');
            if (count($shipingdetails) > 0) {
                $this->pass['shipingdetails'] = $shipingdetails;
                if ($this->input->post()) {
                    $upload = $this->upload_file('image');
                    if ($this->session->userdata('image')) {
                        if (copy(STORE_PATH . "/temp/" . $this->session->userdata('image'), STORE_PATH . "/" . $this->session->userdata('image'))) {
                            unlink(STORE_PATH . "/temp/" . $this->session->userdata('image'));
                            $save['file'] = $this->session->userdata('image');
                            if ($this->itemsent->save($save, array('seller_items_id' => $id), true)) {
                                $this->themes->set_message(array('type' => 'success', 'url' => current_url(), 'message' => 'message_save_success'));
                            }
                        }
                    }
                }
                $this->themes->render('store/showshiping', $this->pass);
                return;
            }
            if ($this->input->post()) {
                $this->load->library('form_validation');
                $this->form_validation->set_rules('company', lang(''), 'trim|required');
                if ($this->form_validation->run()) {
                    $upload = $this->upload_file('image');
                    if ($this->session->userdata('image')) {
                        if (copy(STORE_PATH . "/temp/" . $this->session->userdata('image'), STORE_PATH . "/" . $this->session->userdata('image'))) {
                            unlink(STORE_PATH . "/temp/" . $this->session->userdata('image'));
                            $save['file'] = $this->session->userdata('image');
                        }
                    }
                    $date = strtotime($this->input->post('day') . '/' . $this->input->post('month') . '/' . $this->input->post('year') . ' ' . $this->input->post('hour') . ':' . $this->input->post('minutes') . $this->input->post('type'));
                    $date = date('Y-m-d H:i:s', $date);
                    $save['seller_items_id'] = $id;
                    $save['track_num'] = $this->input->post('tracknum');
                    $save['company'] = $this->input->post('company');
                    $save['company_site'] = $this->input->post('companysite');
                    $save['sent_date'] = $date;
                    if ($this->itemsent->save($save)) {
                        $this->session->unset_userdata('image');
                        $this->session->unset_userdata('post');
                        $save = NULL;
                        $save['sent'] = 'YES';
                        $save['sent_date'] = $date;
                        $this->dealsmo->save($save, array('id' => $id), TRUE);
                        $this->themes->set_message(array('type' => 'success', 'url' => __CLASS__ . '/deals', 'message' => 'message_save_success'));
                    } else {
                        get_save_error();
                    }
                } else {
                    get_validation_errors();
                }
            }

            $this->themes->render('store/shipingdetails', $this->pass);
            return;
        }
        show_404();
    }

    function upgrade() {
        $this->pass['store'] = $store = $this->stores->is_found(array('owner_id' => $this->user_id), array(), 'row', $this->agent->referrer());
        $this->load->model('packages_details_model', 'pdetails');
        $this->load->model('packages_model', 'packages');
        $this->load->model('upgrade_store_model', 'ustore');
        $ustore = $this->ustore->get(array('owner_id' => $this->user_id), false, 'row');
        if (count($ustore) > 0) {
            $this->themes->set_message(array('type' => 'error', 'url' => __CLASS__, 'message' => 'message_you_have_upgrade'));
        }
        $pack = $this->packages->get(array('id' => $store->package_id), false, 'row');
        $this->pass['packages'] = $this->packages->get(array('order >' => $pack->order));
        $this->pass['pdetails'] = $this->pdetails->get();
        if ($this->input->post('package')) {
            $this->load->model('invoicies_model', 'invoice');
            $package = $this->packages->is_found(array('id' => $this->input->post('package')), array(), 'row', current_url());
            $invoice['inv_num'] = $this->unique_id();
            $invoice['user_id'] = $this->user_id;
            if ($store->package_price == 0) {
                $must = $package->price;
            } else {
                $startdate = strtotime($store->start_date);
                $difernce = time() - $startdate;
                $difernce = (int) ($difernce / 2628000);
                $pricemonth = (int) ($store->package_price / 12);
                $timepaied = $difernce * $pricemonth;
                $must = $package->price - $timepaied;
                if ($must <= 0) {
                    $sstore['owner_id'] = $this->user_id;
                    $sstore['package_id'] = $package->id;
                    $sstore['package_values'] = $package->values;
                    $sstore['package_price'] = $package->price;
                    $sstore['invoice_id'] = '0';
                    $sstore['start_date'] = date("Y-m-d H:i:s");
                    $sstore['end_date'] = date("Y-m-d H:i:s", time() + 31536000);
                    if ($this->stores->save($sstore, array('owner_id' => $this->user_id), TRUE)) {
                        $this->themes->set_message(array('type' => 'success', 'url' => __CLASS__, 'message' => 'message_upgrade_sucess'));
                    } else {
                        $this->themes->set_message(array('type' => 'error', 'url' => __CLASS__, 'message' => 'message_upgrade_error'));
                    }
                }
            }
            $invoice['total'] = $must;
            $invoice['must_pay'] = $must;
            $invoice['title'] = 'label_upgrade_store';
            $invoice['for'] = 'upgrade_store';
            if ($this->invoice->save($invoice)) {
                $sstore['owner_id'] = $this->user_id;
                $sstore['package_id'] = $package->id;
                $sstore['package_values'] = $package->values;
                $sstore['package_price'] = $package->price;
                $sstore['invoice_id'] = $invoice['inv_num'];
                if ($this->ustore->save($sstore)) {
                    $this->themes->set_message(array('type' => 'success', 'url' => __CLASS__, 'message' => 'message_upgrade_sucess'));
                } else {
                    $this->themes->set_message(array('type' => 'error', 'url' => __CLASS__, 'message' => 'message_upgrade_error'));
                }
            }
        }

        $this->themes->render('store/upgrade', $this->pass);
    }

    private function unique_id() {
        $string = strrev(md5(time() + $this->session->userdata('session_id')));
        $unique = substr($string, 0, 9);
        $temp = $this->invoice->get(array('inv_num' => $unique));
        if (count($temp) > 0) {
            $this->unique_id();
        } else {
            return $unique;
        }
        return false;
    }

}
